package com.changpei.common.utils;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 *
 *
 * @author Way on 2021/9/15
 */
public class IdsUtil {

    /**
     * 获取随机ID
     * @param size 获取多少个
     * @param total 获取total范围内
     * @return
     */
    public static Set<Long> getIds(Integer size, Long total) {
        Random rand = new Random();
        Set<Long> ids = new HashSet<>();

        if (size > total) {
            return new HashSet<>();
        }

        while (ids.size() < size) {
            // 产生一个随机数，添加到集合
            int number = rand.nextInt(Integer.parseInt(total.toString())) + 1;
            ids.add(Long.parseLong(String.valueOf(number)));
        }

        return ids;
    }

    public static void main(String[] args) {
        Set<Long> ids = getIds(50, 528L);
        System.out.println(ids);
        System.out.println(ids.size());
    }
}
